﻿<%@ Page Title="数据字典" Language="C#" MasterPageFile="~/master/m.master" %>
<script runat="server"></script>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<link href="../assets/css/plugins/jsTree/style.min.css" rel="stylesheet">
<link href="../assets/css/plugins/footable/footable.core.css" rel="stylesheet">
<link href="../assets/css/plugins/iCheck/custom.css" rel="stylesheet">
<link href="../assets/img/validate/validate.css" rel="stylesheet">
<style type="text/css">
.module-info td .control-label {
    width: 90px;
    text-align: right;
}
@media (min-width: 768px) {
	.modal-dialog {
		width: 768px;
		margin: 30px auto;
	}
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="main" Runat="Server">
<div class="system-box">
	<div class="left-side ibox-content">
		<div id="using_json"></div>
	</div>
	<div class="right-side">
		<div class="btns-container">
			<div class="modal inmodal" id="addWindow" tabindex="-1" role="dialog" aria-hidden="true">
		    	<form id="checkModuleForm" method="get" action="#">
					<div class="modal-dialog">
		        		<div class="modal-content animated bounceInRight">
		            		<div class="modal-header">
		                    	<span class="editTitle">新增模块</span>
		                    	<button type="button" class="close" data-dismiss="modal">
		                    		<span aria-hidden="true">&times;</span>
		                        	<span class="sr-only">关闭</span>
		                        </button>
		                    </div>
		                    <div id="message" style="display: none"></div>
		                    <div class="modal-body">
		                    	<table class="module-info" cellpadding="0" cellspacing="0">
		                        	<thead></thead>
		                        	<tbody>
		                        		<tr>
											<td colspan="2">
												<div class="form-group">
													<label class="control-label">字典名称&nbsp;&nbsp;</label>
													<input type="hidden" id="parentValue" />
													<input type="hidden" id="oldDictName" />
													<input type="text" id="DictName" name="DictName" placeholder="请输入字典名称" 
														datacol="yes" err="字典名称" checkexpession="NotNull" 
														maxlength="25" class="form-control input-w630 required" />
												</div>
											</td>
										</tr>
		                            	<tr>
											<td colspan="2">
												<div class="form-group">
													<label class="control-label">　字典值&nbsp;&nbsp;</label>
													<input type="hidden" id="oldDictValue" />
													<input type="text" id="DictValue" name="DictValue" placeholder="请输入字典值" 
														datacol="yes" err="字典值" checkexpession="NotNull" 
														maxlength="30" class="form-control input-w630 required" />
												</div>
											</td>
										</tr>
										<tr>
											<td colspan="2">
												<div class="form-group">
													<label class="control-label">能否修改&nbsp;&nbsp;</label>
													<input type="hidden" id="oldIsModify" />
                                                    <div class="checkbox i-checks label-inline">
                                                        <label class="effective">
                                                            <input id="isModify" type="checkbox" checked="">
                                                        </label>
                                                    </div>
                                                    <div class="label-text label-inline">
                                                        <i class="fa fa-lightbulb-o "></i> 注意：不打勾的话，保存后将不能进行修改。
                                                    </div>

												</div>
											</td>
										</tr>
		                                <tr>
											<td colspan="2">
												<div class="form-group">
													<label class="control-label label-float">备注说明&nbsp;&nbsp;</label>
													<input type="hidden" id="oldMemo" />
													<textarea id="Memo" name="Memo" style="margin-left: 3px;"
														 class="form-control input-textarea input-w630" placeholder="在此填写备注说明"></textarea>
												</div>
											</td>
										</tr>
		                        	</tbody>
		                    	</table>
		                	</div>
		                	<div class="modal-footer">
		                    	<button type="button" id="addModuleBtn" class="btn btn-primary">保存</button>
		                    	<button type="button" id="closeAddBox" class="btn btn-white" data-dismiss="modal">关闭</button>
		                    </div>
		                </div>
		            </div>
		        </form>
		    </div>
		</div>
		<div class="right-content-box">
            <div class="ibox-content">
				<table class="footable table table-stripped table-list">
					<thead></thead>
					<tbody id="pageList"></tbody>
				</table>
	        </div>
        </div>
	</div>
</div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="footer" Runat="Server">
<script src="../assets/js/plugins/jsTree/jstree.min.js"></script>
<script src="../assets/js/plugins/iCheck/icheck.min.js"></script>
<script src="../assets/js/plugins/footable/footable.all.min.js"></script>
<script src="../assets/js/page/rScroll.js"></script>
<script src="../assets/js/page/tableControl.js"></script>
<script src="../assets/img/validate/learunui-validator.js"></script>
<script>
$(document).ready(function(){
	//限制textarea输入字数
	commonUtil.inputLimitForId('Memo',50);
	//初始化多选框
	$(".i-checks").iCheck({
		checkboxClass:"icheckbox_square-green",
	});

	//模块ID
	var ModuleID = Y.getUrlParam("ModuleID");
	//用于记录当前选中节点字典值（右侧）
	var rdictValue = 0;
	//用于记录当前选中节点字典Id
	var rdID = 0;
	//用于记录当前选中节点父值
	var parentDictValue = -1;
	//用于记录当前选中节点父Id
	var parentID = -1;
	//用于记录当前选中节点的Id
	var dictID = '';
	//用于记录当前选中节点的名称
	var dictName = '';
	//用于记录当前选中节点的值
	var dictValue = '';
	//用于标记是新增还是编辑
	var isCreate = true;
	//初始树形列表
	var initTree = true;
	var memo = '';
	var isModify = true;
    //刷新按钮覆盖redis值
    var isType = 0;
    //获取按钮
    commonUtil.loadBtnList(ModuleID, function () {
        //禁用编辑和删除
        $('#editBtn').attr('disabled', 'disabled');
        $('#deleteBtn').attr('disabled', 'disabled');
        rScroll();
    });
	
	//加载左侧列表
	var loadList = function(init) {
		rdID = 0;
		//调用加载层
		layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
		
		initTree = true;
		Y.API2('DwGlobalVariable', 'GetGlobalVariableTreeList', { type: isType }, function (data) {
			//关闭加载层
			layer.closeAll('dialog');
			
			var treeData = [];
		    var temp = {}
		    temp.text = '根目录';
		    temp.data = {
		        dictID: -1,
		        dictValue: -1,
		        dictName: '根目录',
		        parentDictValue: -1,
		        isModify: false
		    };
		    temp.state = { opened: true };

			if (!Y.checkNull(data) && data.length > 0) {
			    //json处理
			    var children = [];
			    data.map(function(item) {
			        var te = {}
			        te.text = item.dictName;
			        te.data = {
			            dictID: item.ID,
			            parentDictValue: item.parentDictValue,
			            dictValue: item.dictValue,
			            dictName: item.dictName,
			            memo: item.Memo,
			            isModify: item.isModify,
			        };
			        te.state = { opened: false };
			        var child = [];
			        item.children.map(function(it) {
			            var t = {}
			            t.text = it.dictName;
			            t.data = {
//							parentID: it.ParentID,
//							dictID: it.DictID,
			                parentDictValue: it.parentDictValue,
			                dictValue: it.dictValue,
			                dictName: it.dictName,
			                memo: it.Memo,
			                isModify: it.isModify,
			            };
			            t.state = { opened: false };
			            child.push(t);
			        });
			        te.children = child;
			        children.push(te);
			    })

			    temp.children = children;
			    treeData.push(temp);

			    $.jstree.destroy("#using_json");
			    $('#using_json').jstree({
			        core: {
			            data: treeData
			        }
			    }).bind('select_node.jstree',
			        function(event, data) {
			            initTree = false;
			            //绑定的点击事件
			            var node_data = data.node.data;
			            console.log(node_data);
			            parentID = node_data.parentID;
			            dictID = node_data.dictID;
			            parentDictValue = node_data.parentDictValue;
			            dictName = node_data.dictName;
			            dictValue = node_data.dictValue;
			            memo = node_data.memo;
			            isModify = node_data.isModify;

			            //如果是根节点则禁用编辑和删除
			            if (parentDictValue == null || parentDictValue == -1) {
			                $('#editBtn').attr('disabled', 'disabled');
			                $('#deleteBtn').attr('disabled', 'disabled');
			                $('#addBtn').removeAttr('disabled');
			            } else {
			                if (!isModify) {
			                    $('#editBtn').attr('disabled', 'disabled');
			                    $('#deleteBtn').attr('disabled', 'disabled');
			                } else {
			                    $('#editBtn').removeAttr('disabled');
			                    $('#deleteBtn').removeAttr('disabled');
			                }
			                $('#addBtn').attr('disabled', 'disabled');
			            }

			            //加载右侧列表
			            loadPageList(dictValue, parentDictValue == -1);
			        });

			    //判断是否为初次调用
			    if (!Y.checkNull(init)) {
			        //获取初始化的相关属性
//					parentID = -1;
			        dictID = -1;
			        parentDictValue = -1;
			        dictName = 'KM软件平台';
			        dictValue = 'KM软件平台';
			        $('#editBtn').attr('disabled', 'disabled');
			        $('#deleteBtn').attr('disabled', 'disabled');

			        //加载右侧列表
			        loadPageList(dictValue, true);
			    }
			} else {
			    treeData.push(temp);
			    $.jstree.destroy("#using_json");
			    $('#using_json').jstree({
			        core: {
			            data: treeData
			        }
			    }).bind('select_node.jstree',
			        function (event, data) {
			            initTree = false;
			            //绑定的点击事件
			            var node_data = data.node.data;
			            console.log(node_data);
			            parentID = node_data.parentID;
			            dictID = node_data.dictID;
			            parentDictValue = node_data.parentDictValue;
			            dictName = node_data.dictName;
			            dictValue = node_data.dictValue;
			            memo = node_data.memo;
			            isModify = node_data.isModify;
			        });


			}
		});

	}
	loadList(true);
	
	//初始化左侧菜单子节点
	var getChildNode = function(data){
		var tempArr = [];
		data.map(function(item,i){
			var temp = {};
			temp.text = item.dictName;
			if(item.Type == 2){
				temp.icon = 'none';
			}
			temp.data = {
				parentID: Y.checkNull(item.ParentID) && item.ParentID != 0 ? -1 : item.ParentID,
				dictID: item.DictID,
				parentDictValue: item.parentDictValue,
				dictValue: item.dictValue,
				dictName: item.dictName,
				memo: item.Memo
			};
			if(!Y.checkNull(item.Childrens) && item.Childrens.length > 0){
				temp.state = {opened: false};
				temp.children = getChildNode(item.Childrens);
			}
			tempArr.push(temp);
		});
		return tempArr;
	}
	
	//获取右侧内容列表
	var loadPageList = function (dictValue,init) {
	    if($('.footable thead').children().size() == 0)
	    	tableControl.initThead2('.footable',['字典名字','字典值','状态','备注说明']);
	 	
	    Y.API2('DwGlobalVariable', (init ? 'GetParentGlobalVariableList' : 'GetGlobalVariableList'), { dictValue: dictValue, type: isType }, function (data) {
			if(data.Success == false){
				swal(data.Message, '', 'error');
			}else{
				var tBody = [];
			    if (!Y.checkNull(data)) {
			    	data.map(function(item,i){
						var tempTr = {
							trClass: (i % 2 == 0 ? 'gradeX footable-even' : 'gradeX footable-odd' ),
							children: [
								{tdText: (Y.checkNull(item.parentDictValue) ? -1 : item.parentDictValue), c:0 , r:0, tdHtml:'', tdId:'', tdClass:'parentDictValue hide'},
								{tdText: item.isModify, c:0 , r:0, tdHtml:'', tdId:'', tdClass:'isModify hide'},
								{tdText: item.DictID, c:0 , r:0, tdHtml:'', tdId:'', tdClass:'rdID hide'},
								{tdText: (item.dictName || ' '), c:0 , r:0, tdHtml:'', tdId:'', tdClass:'dictName'},
								{tdText: (item.dictValue), c:0 , r:0, tdHtml:'', tdId:'', tdClass:'dictValue'},
								{tdText: (item.isModify ? ' 可以修改' : ' 不能修改'), c:0 , r:0, tdHtml: ('<i class="fa fa-'+ (item.isModify ? 'check text-navy' : 'times text-danger') +'"></i>'), tdId:'', tdClass:''},
								{tdText: (item.Memo || ' '), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'dictMemo' },
							]
						};
						tBody.push(tempTr);
					})
				}
			    tableControl.initTbody('.footable',(tBody.length > 0 ? tBody : null),3,null);
			}
		});
	}
	
	//刷新按钮
	$('.btns-container').on('click', '#refreshBtn', function () {
	    isType = 1;
		loadList(true);
	});
	//新增按钮
	$('.btns-container').on('click', '#addBtn', function () {
		if(initTree){
			swal('请先在目录树选择节点');
			return false;
		}
		$('.editTitle').text('新增模块');
		isCreate = true;
		$(':input', '#checkModuleForm').not(':button, :submit, :reset').val('');
		$('#Memo').removeAttr('disabled');
		$('#isModify').removeAttr('disabled');
		if(!$('#isModify').is(':checked'))
			$('#isModify').parent().click();
		if(parentDictValue == -1){
			$('#Memo').attr('disabled','disabled');
			if ($('#isModify').is(':checked'))
			    $('#isModify').parent().click();
			$('#isModify').attr('disabled','disabled');
		}
	});
	//编辑按钮
	$('.btns-container').on('click', '#editBtn', function () {
		$('.editTitle').text('编辑模块');
		isCreate = false;
//		var rd = ($('#pageList .check').size() == 0 ? dictID : rdID);
		//获取详细信息
		layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
		var dn = dictName;
		var pdv = parentDictValue;
		if(!$('#isModify').is(':checked'))
			$('#isModify').parent().click();
		if($('#pageList .check').size() > 0){
			dn = $('#pageList .check').find('.dictName').text();
			pdv = $('#pageList .check').find('.parentDictValue').text();
		}
		Y.API2('DwGlobalVariable', 'GetGlobalVariable', {
			dictName: dn,
			parentDictValue: pdv
		}, function (data) {
			layer.closeAll('dialog');
			if (data.Success == false) {
				swal(data.Message, '', 'error');
			} else {
				$('#parentValue').val(data.parentDictValue);
				$('#DictName').val(data.dictName);
				$('#oldDictName').val(data.dictName);
				$('#DictValue').val(data.dictValue);
				$('#oldDictValue').val(data.dictValue);
				$('#Memo').val(data.Memo);
				$('#oldMemo').val(data.Memo);
			}
		});
		$('#Memo').removeAttr('disabled');
	});
	//删除按钮
	commonUtil.initDeleteBtnEvent({
		callBack: function () {
			var submitJson = {
				oldDict: {
					dictName: dictName,
					dictValue: dictValue,
					Memo: memo == null ? memo : memo.trim(),
					isValid: 0,
					isModify: isModify,
					parentDictValue: parentDictValue,
				},
				newDict: {
					dictName: dictName,
					dictValue: dictValue,
					Memo: memo == null ? memo : memo.trim(),
					isValid: 1,
					isModify: isModify,
				}
			}
			
			if($('#pageList .check').size() > 0){
				var tr = $('#pageList .check');
				submitJson = {
					oldDict: {
						dictName: tr.find('.dictName').text(),
						dictValue: tr.find('.dictValue').text(),
						Memo: tr.find('.dictMemo').text().trim(),
						isValid: 0,
						isModify: tr.find('.isModify').text(),
						parentDictValue: tr.find('.parentDictValue').text(),
					},
					newDict: {
						dictName: tr.find('.dictName').text(),
						dictValue: tr.find('.dictValue').text(),
						Memo: tr.find('.dictMemo').text().trim(),
						isValid: 1,
						isModify: tr.find('.isModify').text(),
					}
				}	
			}
			
			Y.API2('DwGlobalVariable', 'ModifyChildrenGlobalVariable', submitJson, function (data) {
				layer.closeAll('dialog');
				if (data.Success == false) {
					swal('删除失败', data.Message, 'error');
				} else {
					swal('删除成功', '该信息已被你永久删除', 'success');
					loadList(true);
				}
			});
			
		}
	});
	
	//点击右侧列表项
	$('#pageList').on('click','tr',function(){
		$('#pageList tr').removeClass('check');
		$(this).addClass('check');
//		rdID = parseInt($(this).find('.rdID').text());
		
		parentDictValue = $(this).find('.parentDictValue').text();
		//如果是根节点则禁用编辑和删除
//		if(rdID == -1 || isNaN(rdID)){
		if($(this).find('.parentDictValue').size() == 0 || $(this).find('.isModify').text() != 'true'){
			$('#editBtn').attr('disabled','disabled');
			$('#deleteBtn').attr('disabled','disabled');
		}else{
			$('#editBtn').removeAttr('disabled');
			$('#deleteBtn').removeAttr('disabled');
		}
	});
	
	//检验提交信息
	$('#addModuleBtn').click(function(){
		if (!CheckDataValid('#checkModuleForm')) {
            return false;
        }
		//检验通过后执行新增或者编辑
		var successText = '新增成功';
		var did = dictID;
		var pid = isCreate ? dictID : parentID;
		var deal = '';
		var submitJson = {};
		submitJson.dictName = $('#DictName').val();
		submitJson.dictValue = $('#DictValue').val();
		if(!isCreate){
			successText = '修改成功';
			deal = 'ModifyChildrenGlobalVariable';
			submitJson = {
				oldDict: {
					dictName: $('#oldDictName').val(),
					dictValue: $('#oldDictValue').val(),
					Memo: $('#oldMemo').val(),
					isValid: 0,
					isModify: true,
					parentDictValue: $('#parentValue').val(),
				},
				newDict: {
					dictName: $('#DictName').val(),
					dictValue: $('#DictValue').val(),
					Memo: $('#Memo').val(),
					isValid: 0,
					isModify: $('#isModify').is(':checked'),
				}
			}
		}else{
			if(parentDictValue == null){
				deal = 'CreateChildrenGlobalVariable';
				submitJson.parentDictValue = dictValue;
				submitJson.Memo = $('#Memo').val();
				submitJson.isModify = $('#isModify').is(':checked');
			}else{
				deal = 'CreateParentGlobalVariable';
			}
		}
		layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
		Y.API2('DwGlobalVariable', deal, submitJson,function(data){
			layer.closeAll('dialog');
			if(data.Success == false){
				swal(data.Message, '', 'error');
			}else{
				swal(successText, '', 'success');
				//关闭新建  or 编辑框
				$('#closeAddBox').click();
				//刷新列表
				loadList(true);
			}
		})
	});
})
</script>
</asp:Content>

